import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({Key? key}) : super(key: key);

  @override
  _DialogPageState createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  //一种是直接在button中进行事件处理,一种就是这种定义async和await获取result结果以后进行进一步处理
  _alertDialog() async{
    var result = await showDialog(
      context: context,
      builder: (context){
        return AlertDialog(
          title:Text('提示信息'),
          content:Text('确认删除吗？'),
          actions: [
            TextButton(onPressed: () {
              Navigator.pop(context,'cancel');
            }, child: Text('取消')),
            TextButton(onPressed: () {
              Navigator.pop(context,'ok');
            }, child: Text('确定')),
          ],
        );
      }
    );
    print(result);
  }

  //select单选按钮框
  _simpleDialog() async{
    var result = await showDialog(
      context: context,
      builder: (context){
        return SimpleDialog(
          title: Text('select单选按钮框'),
          children: [
            SimpleDialogOption(
              child: Text('Option A'),
              onPressed: (){
                Navigator.pop(context,'A');
              },
            ),
            Divider(),
            SimpleDialogOption(
              child: Text('Option B'),
              onPressed: (){
                Navigator.pop(context,'B');
              },
            ),
            Divider(),
            SimpleDialogOption(
              child: Text('Option C'),
              onPressed: (){
                Navigator.pop(context,'C');
              },
            ),
          ],
        );
      }
    );
    print(result);
  }

  //底部弹出框
  _modalBottomSheet() async{
    var result = await showModalBottomSheet(
      context: context,
      builder: (context){
        return Container(
          height: 200,
          child: Column(
            children: [
              ListTile(
                title: Text('分享到QQ'),
                onTap: (){
                  Navigator.pop(context,1);
                },
              ),
              Divider(),
              ListTile(
                title: Text('分享到微信'),
                onTap: (){
                  Navigator.pop(context,2);
                },
              ),
              Divider(),
              ListTile(
                title: Text('分享到新浪微博'),
                onTap: (){
                  Navigator.pop(context,3);
                },
              )
            ],
          ),
        );
      }
    );
    print('输出${result}');
  }


  _toast() async{
    Fluttertoast.showToast(
        msg: "提示信息！",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('对话'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              child: Text('alert弹出框-AlertDialog'),
              onPressed: _alertDialog,
            ),
            SizedBox(height: 20),
            ElevatedButton(
              child: Text('select弹出框-SimpleDialog'),
              onPressed: _simpleDialog,
            ),
            SizedBox(height: 20),
            ElevatedButton(
              child: Text('ActionSheet底部弹出框-showModalBottomSheet'),
              onPressed: _modalBottomSheet,
            ),
            SizedBox(height: 20),
            ElevatedButton(
              child: Text('toast-fluttertoast第三方库'),
              onPressed: _toast,
            ),
            SizedBox(height: 20),
          ],
        ),
      ),
    );
  }
}
